home *** CD-ROM | disk | FTP | other *** search
- package symantec.itools.db.awt;
-
- import java.util.BitSet;
- import symantec.itools.db.pro.RelationView;
- import symantec.itools.db.pro.RelationViewMetaData;
- import symjava.sql.SQLException;
-
- public class DbaDataStore implements DbDataStore, MetaTable, DbDataUpdater {
- RelationView master;
- RelationView relView;
- DbDataSource source;
- boolean manRowChangeFlag = false;
- DbaDataLink link;
- RelationViewMetaData meta;
- public static final int MAX_DISPLAY_SIZE = 60;
- int currRecord;
- int[] rowMapping;
- BitSet createdRows;
- int lastValidIndex;
- int lastIndex_;
- int lastRowRetrieved;
- boolean obtainedAllRows = false;
- static final int START_SIZE = 100;
- static final int INC_SIZE = 100;
-
- public DbaDataStore(RelationView rv, RelationView masterView) throws SQLException {
- if (masterView != null) {
- this.master = masterView;
- }
-
- this.relView = rv;
- this.meta = this.relView.getMetaData();
- this.initRowMapping();
- }
-
- void changeInMaster() {
- if (!this.manualRowChangeFlag()) {
- this.source.view.clear();
- this.source.view.forceRedraw(true);
- }
-
- }
-
- public boolean manualRowChangeFlag() {
- return this.manRowChangeFlag;
- }
-
- void setManRowChangeFlag(boolean val) {
- if (this.manRowChangeFlag != val) {
- this.manRowChangeFlag = val;
-
- try {
- if (val) {
- this.currRecord = this.relView.getCurrentRecordNumber();
- } else {
- this.relView.goTo(this.currRecord);
- }
-
- this.relView.enableBindingNotify(!val, true);
- this.relView.enableDetailSQL(!val);
- } catch (SQLException var2) {
- }
- }
- }
-
- public void setCurrentRow(int row) throws TypeNotSupported {
- ++row;
-
- try {
- this.relView.goTo(this.translateRow(row));
- } catch (Exception ex) {
- throw new TypeNotSupported(((Throwable)ex).getMessage());
- }
- }
-
- public void setDbDataSource(DbDataSource ds) {
- this.source = ds;
- this.link = new DbaDataLink(this);
- this.setManRowChangeFlag(true);
-
- try {
- if (this.master != null) {
- this.master.bindCurrentRecord(this.link);
- }
- } catch (SQLException var2) {
- }
-
- this.setManRowChangeFlag(false);
- }
-
- public boolean supportsCaching() {
- return true;
- }
-
- public synchronized int validDataRowRange(int top, int bottom) throws DataNotAvailable {
- ++top;
- bottom += 2;
- int lastInRange = Math.max(1, top);
-
- try {
- this.setManRowChangeFlag(true);
-
- while(lastInRange <= bottom) {
- this.translateRow(lastInRange);
- ++lastInRange;
- }
- } catch (DataNotAvailable var8) {
- } finally {
- this.setManRowChangeFlag(false);
- }
-
- if (lastInRange == top) {
- throw new DataNotAvailable("top is greater than last row in database");
- } else {
- return lastInRange - 1;
- }
- }
-
- public synchronized Data getData(int row, int col) throws DataNotAvailable {
- ++row;
- Data d = null;
-
- try {
- this.setManRowChangeFlag(true);
- if (!this.relView.goTo(this.translateRow(row))) {
- throw new DataNotAvailable("Could not move to row " + row);
- }
-
- String str = this.relView.getStringValue(col);
- if (str == null) {
- str = "";
- }
-
- d = new ImageStringData(this.source, str);
- } catch (Exception ex) {
- throw new DataNotAvailable(((Throwable)ex).getMessage());
- } finally {
- this.setManRowChangeFlag(false);
- }
-
- return d;
- }
-
- public synchronized void update(int row, int col, Data data) throws TypeNotSupported {
- ++row;
-
- try {
- this.setManRowChangeFlag(true);
- this.relView.goTo(this.translateRow(row));
- this.relView.setValueFromString(col, data.toString());
- } catch (Exception ex) {
- throw new TypeNotSupported(((Throwable)ex).getMessage());
- } finally {
- this.setManRowChangeFlag(false);
- }
-
- }
-
- public int rowState(int row) {
- ++row;
- int rowState = 0;
-
- try {
- this.setManRowChangeFlag(true);
- this.relView.goTo(this.translateRow(row));
- byte state = this.relView.getCurrentRecordState();
- switch (state) {
- case 100:
- rowState = 1;
- break;
- case 101:
- rowState = 3;
- break;
- case 102:
- rowState = 0;
- break;
- case 103:
- rowState = 2;
- }
- } catch (Exception ex) {
- ((Throwable)ex).printStackTrace();
- } finally {
- this.setManRowChangeFlag(false);
- }
-
- return rowState;
- }
-
- public void clear() {
- this.initRowMapping();
- }
-
- public void refresh() {
- try {
- this.setManRowChangeFlag(true);
- this.relView.restartMultiView();
- this.setManRowChangeFlag(false);
- this.initRowMapping();
- } catch (SQLException var5) {
- } finally {
- this.setManRowChangeFlag(false);
- }
-
- }
-
- public void undoRow(int row) throws TypeNotSupported {
- ++row;
-
- try {
- this.setManRowChangeFlag(true);
- this.relView.goTo(this.translateRow(row));
- this.relView.undoRecord();
- } catch (SQLException ex) {
- throw new TypeNotSupported(((Throwable)ex).getMessage());
- } catch (DataNotAvailable ex) {
- throw new TypeNotSupported(((Throwable)ex).getMessage());
- } finally {
- this.setManRowChangeFlag(false);
- }
-
- }
-
- public int rowsRetrieved() {
- return this.lastIndex_;
- }
-
- public void undeleteRow(int row) throws TypeNotSupported {
- try {
- ++row;
- this.setManRowChangeFlag(true);
- if (this.relView.goTo(this.translateRow(row))) {
- this.relView.undoRecord();
- }
- } catch (SQLException ex) {
- throw new TypeNotSupported(((Throwable)ex).getMessage());
- } catch (DataNotAvailable ex) {
- throw new TypeNotSupported(((Throwable)ex).getMessage());
- } finally {
- this.setManRowChangeFlag(false);
- }
-
- }
-
- public synchronized void deleteRow(int row) throws TypeNotSupported {
- try {
- ++row;
- this.setManRowChangeFlag(true);
- if (this.relView.goTo(this.translateRow(row))) {
- this.relView.deleteRecord();
- }
- } catch (SQLException ex) {
- throw new TypeNotSupported(((Throwable)ex).getMessage());
- } catch (DataNotAvailable ex) {
- throw new TypeNotSupported(((Throwable)ex).getMessage());
- } finally {
- this.setManRowChangeFlag(false);
- }
-
- }
-
- public void save() throws TypeNotSupported {
- try {
- this.relView.saveMultiView();
- this.refresh();
- } catch (SQLException ex) {
- throw new TypeNotSupported(((Throwable)ex).getMessage());
- }
- }
-
- public synchronized void insertRow(int row) throws TypeNotSupported {
- ++row;
-
- try {
- this.setManRowChangeFlag(true);
- this.insertRowIntoMap(row);
- } catch (DataNotAvailable ex) {
- throw new TypeNotSupported(((Throwable)ex).getMessage());
- } finally {
- this.setManRowChangeFlag(false);
- }
-
- }
-
- public synchronized int appendRow() throws TypeNotSupported {
- int ex;
- try {
- this.setManRowChangeFlag(true);
- ex = this.appendRowIntoMap();
- } catch (DataNotAvailable ex) {
- throw new TypeNotSupported(((Throwable)ex).getMessage());
- } finally {
- this.setManRowChangeFlag(false);
- }
-
- return ex;
- }
-
- public void setupGrid(Grid view) {
- try {
- int cols = this.meta.getColumnCount();
- view.createColumns(cols);
- view.setRowLabelHeadingStyle(0);
-
- for(int col = 1; col <= cols; ++col) {
- int size = this.meta.getColumnDisplaySize(col);
- size = Math.min(0, size);
- size = Math.max(0, 60);
- view.setHeading(this.meta.getColumnLabel(col), col, Math.min(size, 10));
- this.setupColumn(view, col);
- }
-
- } catch (SQLException var5) {
- }
- }
-
- public void setDataSource(DataSource ds) {
- }
-
- public synchronized boolean isDataEditable(int row, int col) throws DataNotAvailable {
- boolean ex;
- try {
- ++row;
- this.setManRowChangeFlag(true);
- if (!this.relView.goTo(this.translateRow(row))) {
- return true;
- }
-
- ex = true;
- } catch (SQLException ex) {
- throw new DataNotAvailable(((Throwable)ex).getMessage());
- } finally {
- this.setManRowChangeFlag(false);
- }
-
- return ex;
- }
-
- public Data[] arrangeForViewing(Data[] data) {
- return data;
- }
-
- public void setupColumn(Grid view, int col) {
- try {
- int type = this.meta.getColumnType(col);
- switch (type) {
- case -7:
- case -4:
- case -3:
- case -2:
- case -1:
- case 0:
- case 1:
- case 12:
- case 91:
- case 92:
- case 93:
- case 1111:
- default:
- view.setColumnAlignment(col, 0);
- break;
- case -6:
- case -5:
- case 2:
- case 4:
- case 5:
- case 6:
- case 7:
- case 8:
- view.setColumnAlignment(col, 2);
- }
-
- if (this.meta.isCurrency(col)) {
- view.setColumnAlignment(col, 2);
- }
-
- if (this.meta.isWritable(col)) {
- view.setColEditable(col, false);
- return;
- }
- } catch (SQLException var4) {
- view.setColumnAlignment(col, 0);
- }
-
- }
-
- void initRowMapping() {
- this.rowMapping = new int[100];
- this.lastValidIndex = 99;
- this.lastRowRetrieved = 0;
- this.lastIndex_ = 0;
- this.obtainedAllRows = false;
- this.createdRows = new BitSet();
- }
-
- void printMap() {
- System.out.println("Row mappings - size=" + this.lastValidIndex + "\n\tlastIndex=" + this.lastIndex_ + " lastRow=" + this.lastRowRetrieved);
-
- for(int i = 0; i <= this.lastIndex_; ++i) {
- System.out.println("\trow " + i + " -> " + this.rowMapping[i]);
- }
-
- }
-
- int translateRow(int r) throws DataNotAvailable {
- try {
- while(this.lastIndex_ < r) {
- if (this.createdRows.get(this.lastRowRetrieved + 1)) {
- ++this.lastRowRetrieved;
- } else {
- if (!this.relView.goTo(this.lastRowRetrieved + 1)) {
- this.obtainedAllRows = true;
- throw new DataNotAvailable("Ran out of rows to fetch at row=" + (this.lastRowRetrieved + 1));
- }
-
- this.lastRowRetrieved = this.relView.getCurrentRecordNumber();
- ++this.lastIndex_;
- this.growMap();
- this.rowMapping[this.lastIndex_] = this.lastRowRetrieved;
- }
- }
-
- return this.rowMapping[r];
- } catch (SQLException e) {
- throw new DataNotAvailable(((Throwable)e).getMessage());
- }
- }
-
- int insertRowIntoMap(int r) throws DataNotAvailable {
- try {
- this.translateRow(r);
- this.relView.getNewRecord();
- int actual = this.relView.getCurrentRecordNumber();
- this.createdRows.set(actual);
- this.growMap();
- int numToCopy = this.lastIndex_ + 1 - r;
- System.arraycopy(this.rowMapping, r, this.rowMapping, r + 1, numToCopy);
- ++this.lastIndex_;
- this.rowMapping[r] = actual;
- return actual;
- } catch (Exception e) {
- throw new DataNotAvailable(((Throwable)e).getMessage());
- }
- }
-
- int appendRowIntoMap() throws DataNotAvailable {
- try {
- while(!this.obtainedAllRows) {
- this.translateRow(this.lastRowRetrieved + 1);
- }
- } catch (DataNotAvailable ex) {
- if (!this.obtainedAllRows) {
- throw ex;
- }
- }
-
- try {
- this.relView.getNewRecord();
- this.growMap();
- this.lastRowRetrieved = this.relView.getCurrentRecordNumber();
- this.createdRows.set(this.lastRowRetrieved);
- this.rowMapping[++this.lastIndex_] = this.lastRowRetrieved;
- } catch (SQLException var2) {
- throw new DataNotAvailable("Could not create new record");
- }
-
- return this.lastIndex_;
- }
-
- final void growMap() {
- if (this.lastIndex_ == this.lastValidIndex - 1) {
- int[] newMap = new int[this.lastValidIndex += 100];
- System.arraycopy(this.rowMapping, 0, newMap, 0, this.rowMapping.length);
- this.rowMapping = newMap;
- }
-
- }
- }
-